package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.FeedBack;
import com.javasm.entity.MisMannageEntity.ReturnCode1;
import com.javasm.entity.MisMannageEntity.ReturnEntity;
import com.javasm.service.MisMannageService.FeedBackService;
import com.javasm.service.MisMannageService.impl.FeedBackServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author:JAVASM
 * @description:
 * @date:2022/5/25 22:14
 * @version:0.1
 * @since:jdk11
 */
@WebServlet("/feedback")
public class FeedbackServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uidStr = req.getParameter("uid");//登录用户ID
        String troubleList = req.getParameter("troubleList");//用户遇到的问题
        String suggestion = req.getParameter("suggestion");//建议
        String contact = req.getParameter("contact");//联系方式
        String platform = req.getParameter("platform");//平台来源
        String osStr = req.getParameter("os");//平台来源：1 IOS，2 Android

        Integer uid = 0;
        if (uidStr!=null && !"".equals(uidStr)){
            uid = Integer.parseInt(uidStr);
        }
        Integer os = 0;
        String osString = null;
        if (osStr!=null && !"".equals(osStr)){
            os = Integer.parseInt(osStr);
            if (os == 1) {
                osString = "IOS";
            } else if (os == 2){
                osString = "Android";
            }
        }

        FeedBack feedBack = new FeedBack(uid,troubleList,suggestion,contact,platform,osString);

        FeedBackService fbs = new FeedBackServiceImpl();
        Integer addFeedBackNum = fbs.addFeedBack(feedBack);

        ReturnEntity re = new ReturnEntity();
        if (addFeedBackNum>0) {
            re.setRetCode(ReturnCode1.OPERATE_OK.getCode());
            re.setRetMsg(ReturnCode1.OPERATE_OK.getMsg());
        }else {
            re.setRetCode(ReturnCode1.OPERATE_FAILED.getCode());
            re.setRetMsg(ReturnCode1.OPERATE_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();


    }
}
